package org.xiph.vorbis
{
	/**
	 * TODO Make commands better accessible...
	 *
	 * @author Eugene Zatepyakin
	 * @author Andre Michelle
	 */
	public final class VorbisInfo
	{
		private var _channels:uint;
		private var _sampleRate:uint;
		private var _bitRateUpper:uint;
		private var _bitRateLower:uint;
		private var _bitRateNominal:uint;
		private var _sampleMultiplier:uint;
		private var _commentList:Array = new Array();
		private var _commentTable:Object = new Object();

		public function VorbisInfo( object: Object )
		{
			var i:uint;
			var max:uint = object.commentList.length;
			var comment:String;
			var equalIndex:int;

			_channels = object.channels;
			_sampleRate = object.sampleRate;
			_sampleMultiplier = 44100.0 / object.sampleRate;
			_bitRateUpper = object.bitRateUpper;
			_bitRateLower = object.bitRateLower;
			_bitRateNominal = object.bitRateNominal;

			for( i = 0; i < max; ++i )
			{
				comment = object.commentList[i];
				_commentList.push( comment );

				equalIndex = comment.indexOf( "=" );

				if( equalIndex > -1 )
				{
					_commentTable[comment.substring( 0, equalIndex )] = comment.substring( equalIndex + 1 );
				}
			}
		}

		public function get channels():uint
		{
			return _channels;
		}

		public function get sampleRate():uint
		{
			return _sampleRate;
		}

		public function get bitRateUpper():uint
		{
			return _bitRateUpper;
		}

		public function get bitRateLower():uint
		{
			return _bitRateLower;
		}

		public function get bitRateNominal():uint
		{
			return _bitRateNominal;
		}

		public function get sampleMultiplier():uint
		{
			return _sampleMultiplier;
		}

		public function get commentList():Array
		{
			return _commentList;
		}

		public function get commentTable():Object
		{
			return _commentTable;
		}

		public function debug(): String
		{
			var result:String = "";
			var p:String;

			result += "channels: " + _channels + "\n";
			result += "sampleRate: " + _sampleRate + "\n";
			result += "bitRateUpper: " + _bitRateUpper + "\n";
			result += "bitRateLower: " + _bitRateLower + "\n";
			result += "bitRateNominal: " + _bitRateNominal + "\n";
			result += "sampleMultiplier: " + _sampleMultiplier + "\n";

			result += "comments: \n";
			for( p in _commentTable )
			{
				result += "\t" + p + ": " + _commentTable[p] + "\n";
			}

			return result;
		}

		public function toString():String
		{
			return '[VorbisInfo]';
		}
	}
}